/** 
 * 战场
 * Author      : suyuyi
 * Create Time : 2017.4.16
 */
cc.Class({
    extends: cc.Component,

    /** 战斗技能按钮 */
    _onTouchEnd : function(event){
        switch(event.target.name){
            case "btn_attack":
                game.battleManager.createBullet();
                break;
            case "btn_speed":
                game.playerComponent.speed();           // 改变玩家飞行速度    
                break;
        }
    },

    onLoad: function () {
        // 定义游戏事件
        cc.loader.loadRes("resources/persist/plane_hero", cc.Prefab,function (err, prefab) {
    // ...
        var player = cc.instantiate(prefab);
        
        
        cc.log(player);
        });
       // game.GameEvent = require("GameEvent");
       var player = new (require("mplayer"))();
        // 攻击键
        this.node.addChild(player);
        cc.find("btn_attack", this.node).on(cc.Node.EventType.TOUCH_END   , this._onTouchEnd, this);
        cc.find("btn_attack", this.node).on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
        
        // 加速键
        cc.find("btn_speed", this.node).on(cc.Node.EventType.TOUCH_END   , this._onTouchEnd, this);
        cc.find("btn_speed", this.node).on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);

        // 纹理图集
        //game.sheet = cc.loader.getRes("persist/plist/sheet", cc.SpriteAtlas);
        //game.play  = game.scene.getLayer(game.SceneLayerType.MAP);
        
        // 创建地图
        cc.find("background",this.node).addComponent("Map1");
        this.map = cc.find("background",this.node);
        //game.play.createMap(2000, 1000);

        // 创建飞机
        this.player = player.createPlayer();
        this.player.on("position-changed", function(event){                         // 飞机移动驱动地图滚屏
            this.map.x = -event.currentTarget.x;
            this.map.y = -event.currentTarget.y;
        }, this);

        // 飞机逻辑组件
        this.playerComponent = this.player.getComponent("plane1");
        // game.playerComponent.setName("飞机");
        // game.playerComponent.setSkin(12);

        // 游戏逻辑事件
        this.node.on("PLAYER_ATTACK", this._onHandler, this);
        this.node.on("ENEMY_HIT"    , this._onHandler, this);
        this.node.on("ENEMY_ADD "   , this._onHandler, this);
        this.node.on("BULLET_ADD "  , this._onHandler, this);

        // 创建战斗管理器
        this.battleManager = new (require('mm'))();
        this.battleManager.setPlayer(this.player);
        this.battleManager.setMapInfo(new cc.Vec2(2000, 1000));

        // 创建敌人
        // var enemy1 = game.play.createEnemy(10, 0, 240);
        // game.battleManager.addEnemy(enemy1);

        // var enemy2 = game.play.createEnemy(20, 0, -240);
        // game.battleManager.addEnemy(enemy2);

        // var enemy3 = game.play.createEnemy(30, 240, -0);
        // game.battleManager.addEnemy(enemy3);

        // var enemy4 = game.play.createEnemy(40, -240, -0);
        // game.battleManager.addEnemy(enemy4);
    

        // 游戏开始
        //game.battleManager.startGame();
    },

    _onHandler : function(event, args){
        switch(event){
            case "PLAYER_ATTACK":          // 玩家开始攻击
                break;
            case "ENEMY_HIT":              // 命中敌人特效
                player.createEffectHit(args.id, args.x, args.y); 
                break;
            case "ENEMY_ADD":              // 添加敌人
                // cc.log('[BattleField _onHandler] ENEMY_ADD, x = {0}, y = {1}'.format(args.x, args.y));
                var enemy = player.createEnemy(args.id, args.x, args.y);
                this.battleManager.addEnemy(enemy);
                break;
            case "BULLET_ADD":             // 添加子弹
                // cc.log('[BattleField _onHandler] BULLET_ADD, x = {0}, y = {1}, direct = {2}'.format(args.x, args.y, args.direct));
                var bullet = player.createBullet(args.id, args.x, args.y, args.direct, args.speed);
                this.battleManager.addBullet(bullet);
                break;
        }
    },
});
